package cn.tedu.kcircle.gateway.filter;

import cn.tedu.kcircle.common.consts.HttpConst;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class ClientInfoFilter implements GlobalFilter, Ordered, HttpConst {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        //添加User-Addr请求头 修改User-Agent请求头
        ServerHttpRequest eRequest = request.mutate().headers(httpHeaders -> {
            httpHeaders.add(HEADER_USER_ADDR, request.getRemoteAddress().getHostName());
//            httpHeaders.set(HEADER_USER_AGENT,request.getHeaders().getFirst(HEADER_USER_AGENT));
        }).build();
        return chain.filter(exchange.mutate().request(eRequest).build());
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
